import 'package:efood_multivendor/controller/category_controller.dart';
import 'package:efood_multivendor/helper/responsive_helper.dart';
import 'package:efood_multivendor/helper/route_helper.dart';
import 'package:efood_multivendor/util/dimensions.dart';
import 'package:efood_multivendor/util/styles.dart';
import 'package:efood_multivendor/view/base/custom_button.dart';
import 'package:efood_multivendor/view/base/custom_image.dart';
import 'package:efood_multivendor/view/base/no_data_screen.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class InterestScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    Get.find<CategoryController>().getCategoryList(true);

    return Scaffold(
      body: SafeArea(
        child: GetBuilder<CategoryController>(builder: (categoryController) {
          return categoryController.categoryList != null
              ? categoryController.categoryList?.isNotEmpty != true
                  ? Center(
                      child: Container(
                        width: Dimensions.WEB_MAX_WIDTH,
                        padding: EdgeInsets.all(Dimensions.PADDING_SIZE_SMALL),
                        child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              SizedBox(height: Dimensions.PADDING_SIZE_LARGE),
                              Text('choose_your_interests'.tr,
                                  style: sfMedium.copyWith(fontSize: 22)),
                              SizedBox(height: Dimensions.PADDING_SIZE_SMALL),
                              Text('get_personalized_recommendations'.tr,
                                  style: sfRegular.copyWith(
                                      color: Theme.of(context).disabledColor)),
                              SizedBox(height: Dimensions.PADDING_SIZE_LARGE),
                              Expanded(
                                child: GridView.builder(
                                  physics: BouncingScrollPhysics(),
                                  itemCount:
                                      categoryController.categoryList?.length ??
                                          0,
                                  gridDelegate:
                                      SliverGridDelegateWithFixedCrossAxisCount(
                                    crossAxisCount:
                                        ResponsiveHelper.isDesktop(context)
                                            ? 4
                                            : ResponsiveHelper.isTab(context)
                                                ? 3
                                                : 2,
                                    childAspectRatio: (1 / 0.35),
                                  ),
                                  itemBuilder: (context, index) {
                                    return InkWell(
                                      onTap: () => categoryController
                                          .addInterestSelection(index),
                                      child: Container(
                                        margin: EdgeInsets.all(Dimensions
                                            .PADDING_SIZE_EXTRA_SMALL),
                                        padding: EdgeInsets.symmetric(
                                          vertical: Dimensions
                                              .PADDING_SIZE_EXTRA_SMALL,
                                          horizontal:
                                              Dimensions.PADDING_SIZE_SMALL,
                                        ),
                                        decoration: BoxDecoration(
                                          color: categoryController
                                                          .interestSelectedList?[
                                                      index] ==
                                                  true
                                              ? Theme.of(context).primaryColor
                                              : Theme.of(context).cardColor,
                                          borderRadius: BorderRadius.circular(
                                              Dimensions.RADIUS_SMALL),
                                          boxShadow: [
                                            BoxShadow(
                                                color: Colors.grey[
                                                    Get.isDarkMode
                                                        ? 800
                                                        : 200]!,
                                                blurRadius: 5,
                                                spreadRadius: 1)
                                          ],
                                        ),
                                        alignment: Alignment.center,
                                        child: Row(children: [
                                          CustomImage(
                                            image: categoryController
                                                .categoryList![index].image!,
                                            height: 30,
                                            width: 30,
                                          ),
                                          SizedBox(
                                              width: Dimensions
                                                  .PADDING_SIZE_EXTRA_SMALL),
                                          Flexible(
                                              child: Text(
                                            categoryController
                                                    .categoryList?[index]
                                                    .name ??
                                                '',
                                            style: sfMedium.copyWith(
                                              fontSize:
                                                  Dimensions.fontSizeSmall,
                                              color: categoryController
                                                              .interestSelectedList?[
                                                          index] ==
                                                      true
                                                  ? Theme.of(context).cardColor
                                                  : Theme.of(context)
                                                      .textTheme
                                                      .bodyMedium
                                                      ?.color,
                                            ),
                                            maxLines: 1,
                                            overflow: TextOverflow.ellipsis,
                                          )),
                                        ]),
                                      ),
                                    );
                                  },
                                ),
                              ),
                              !categoryController.isLoading
                                  ? CustomButton(
                                      buttonText: 'save_and_continue'.tr,
                                      onPressed: () {
                                        List<int> _interests = [];
                                        for (int index = 0;
                                            index <
                                                categoryController
                                                    .categoryList!.length;
                                            index++) {
                                          if (categoryController
                                                      .interestSelectedList?[
                                                  index] ==
                                              true) {
                                            _interests.add(categoryController
                                                .categoryList![index].id!);
                                          }
                                        }
                                        categoryController
                                            .saveInterest(_interests)
                                            .then((isSuccess) {
                                          if (isSuccess) {
                                            Get.offAllNamed(
                                                RouteHelper.getInitialRoute(
                                                    "not found"));
                                          }
                                        });
                                      },
                                    )
                                  : Center(
                                      child: CircularProgressIndicator.adaptive(
                                        backgroundColor: Theme.of(context)
                                            .colorScheme
                                            .secondary,
                                      ),
                                    )
                            ]),
                      ),
                    )
                  : NoDataScreen(text: 'no_category_found'.tr)
              : Center(
                  child: CircularProgressIndicator.adaptive(
                    backgroundColor: Theme.of(context).colorScheme.secondary,
                  ),
                );
        }),
      ),
    );
  }
}
